package 迭代遍历;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author: zhengtao
 * @description: 二叉树的前序遍历
 * @version: 1.0
 * @date: 2022/10/29  09:43
 */
public class Demo144 {
    List<Integer> res=new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while (!stack.empty()){
            TreeNode pop = stack.pop();
            if(pop!=null){
                res.add(pop.val);
                stack.push(pop.right);
                stack.push(pop.left);
            }
        }
        return res;

    }

    /**
     * Definition for a binary tree node.
     * */
      public class TreeNode {
          int val;
          TreeNode left;
          TreeNode right;
          TreeNode() {}
          TreeNode(int val) { this.val = val; }
          TreeNode(int val, TreeNode left, TreeNode right) {
              this.val = val;
              this.left = left;
              this.right = right;
          }
      }

}
